/**
 * 
 */
package cn.antia.cwin;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;
import cn.antia.cep.common.*;
/**
 * @author sunkeei
 * @date Nov 14, 2007
 * @version 0.1
 * @time 
 */
public class SysConf {
	public static String configFilePath;
	public static Properties config;
	//very very important config
	public static String SYSTEM_DB;		//系统数据库类型
	public static String SYSTEM_DB_CONFIG = null;		//系统后台数据库配置文件地址
	public static String SYSTEM_GRID_CONFIG = null;		//系统表格配置文件地址
	public static String SYSTEM_WINDOW_CONFIG = null;		//系统表格配置文件地址
	public static String IP_FILE = null;				//IP File address
	//dis
	public static Integer DEFAULT_DISP_COL;		//默认显示行
	public static String FORM_METHOD;			//表单提交方法
    public static String ACCEPT_URL = "window.was";			//表单提交方法
    //extends
	public static String KEY_FIELD;				//主键字段
	public static String PASSWORD_FIELD;		//加密字段
	//page
	public static String NEW_VALUE;			//新建一行时临时显示值
	//valiables
	public static String[] SYSTEM_VALIABLES;	//系统变量
	public static String SYSTEM_VALIABLE_PREFIX;	//系统变量前缀
	//charset
	public static String CHARSET;
	
	public static void init(String basePath,String file_path){
		configFilePath = file_path;
		AntiaLog.info("configFilePath:" + configFilePath);
		try{
			InputStream inputStream = new FileInputStream(configFilePath);
			config = new Properties();
			config.load(inputStream);
		
			SYSTEM_DB = config.getProperty("db");		//系统数据库类型
			SYSTEM_DB_CONFIG = basePath + config.getProperty("db_config_file");
			SYSTEM_GRID_CONFIG = basePath + config.getProperty("grid_config_file");
			SYSTEM_WINDOW_CONFIG = basePath + config.getProperty("window_config_file");
			IP_FILE = basePath + config.getProperty("ip_file");
			DEFAULT_DISP_COL = config.getProperty("window_col_num") == null ? 2 : new Integer(config.getProperty("window_col_num"));
			FORM_METHOD = config.getProperty("window_method");
			
			KEY_FIELD = config.getProperty("key_field");
			PASSWORD_FIELD = config.getProperty("password");
			//页面新建行时的默认内容
			NEW_VALUE = config.getProperty("new_value");
			
			//system valiables
			SYSTEM_VALIABLES = config.getProperty("valiables").split(",");
			//system valiable prefix
			SYSTEM_VALIABLE_PREFIX = config.getProperty("valiable_prefix");
			//charset
			CHARSET = config.getProperty("charset");
			StringBuffer sb = new StringBuffer("db:").append(SYSTEM_DB).append(" db_config_file:").append(SYSTEM_DB_CONFIG);
			sb.append(" grid_config_file:").append(SYSTEM_GRID_CONFIG).append(" window_config_file:").append(SYSTEM_WINDOW_CONFIG);
			sb.append(" ip_file:").append(IP_FILE).append(" method:").append(FORM_METHOD);
			sb.append(" window_col_num:").append(DEFAULT_DISP_COL).append(" valiables:").append(SYSTEM_VALIABLES.toString());
			AntiaLog.info("System config init finished.." + sb.toString());
		}catch(Exception e){
			e.printStackTrace();
			AntiaLog.fatal("致命错误，系统设置文件读取错误:" + file_path);
		}
	}
	
	/**
     * 判断是否为系统变量
     * @param s s
     * @return boolean
     */
	public static boolean isSystemString(String s){
		if(s == null)return false;
        for (String sv : SYSTEM_VALIABLES) {
            if (s.equals(sv)) {
                return true;
            }
        }
		return false;
	}
}
